package com.sudo.onload;

import com.sudo.common.utils.*;
import com.sudo.mapper.my.*;
import com.sudo.pojo.SysRole;
import com.sudo.pojo.SysRoleButton;
import com.sudo.pojo.SysRoleMenu;
import com.sudo.pojo.SysUserRole;
import com.sudo.pojo.vo.SysMenuButtonVO;
import com.sudo.pojo.vo.SysRoleButtonVO;
import org.apache.commons.lang3.StringUtils;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;

import static com.sudo.common.enums.UserParams.*;

/**
 * @author：为道日损 2020-05-06 19:41
 * webSite：https://www.sudo.ren
 */
@Component
public class OnloadSomething {

    @Autowired
    private SysRoleMapperCustom sysRoleMapperCustom;

    @Autowired
    private SysInfoMapperCustom sysInfoMapperCustom;

    @Autowired
    private RedisOperator redisOperator;

    /**
     * 项目启动加载所有角色对应菜单的按钮权限(可用角色 可用菜单 可用按钮)
     */
    public void onLoadAllRoleMenuButtonAuth(String roleCode,String menuCode) {
        //清空权限缓存
        if (null == roleCode) {
            redisOperator.mdel(redisOperator.keys(REDIS_ROLE_PERMISSION.value + ":*"));
        } else if (null != menuCode) {
            redisOperator.mdel(redisOperator.keys(REDIS_ROLE_PERMISSION.value + ":" + roleCode + ":" + menuCode));
        } else {
            redisOperator.mdel(redisOperator.keys(REDIS_ROLE_PERMISSION.value + ":" + roleCode + ":*"));
        }

        List<SysRoleButtonVO> sysRoleButtonVOList = sysRoleMapperCustom.getRoleMenuButtonAuth(roleCode,menuCode);
        Iterator<SysRoleButtonVO> iterator = sysRoleButtonVOList.iterator();

        while (iterator.hasNext()) {
            SysRoleButtonVO sysRoleButtonVO = iterator.next();
            String key = REDIS_ROLE_PERMISSION.value+":"+sysRoleButtonVO.getRoleCode()+":"+sysRoleButtonVO.getMenuCode();
            if (!redisOperator.hasKey(key)) {
                redisOperator.sadd(key, "none");
            }
            if (StringUtils.isNotBlank(sysRoleButtonVO.getButtonCode())) {
                redisOperator.sadd(key, sysRoleButtonVO.getButtonCode());
            }
        }
    }
    /**
     * 加载可用角色和可用用户
     */
    public void onLoadSysRoleUserToRedis(String roleCode) {
        //清空角色用户缓存
        if (null == roleCode) {
            redisOperator.mdel(redisOperator.keys(REDIS_ROLE_USER.value + ":*"));
        } else {
            redisOperator.mdel(redisOperator.keys(REDIS_ROLE_USER.value + ":"+ roleCode));
        }

        List<SysUserRole> sysUserRoleList = sysRoleMapperCustom.getSysRoleUserCodeByOn(roleCode);
        Iterator<SysUserRole> iterator = sysUserRoleList.iterator();
        while (iterator.hasNext()) {
            SysUserRole sysUserRole = iterator.next();
            redisOperator.sadd(REDIS_ROLE_USER.value+":"+sysUserRole.getRoleCode(), sysUserRole.getUsername());
        }
    }

    /**
     * 加载所有可用菜单的可用按钮
     */
    public void onLoadMenuButtonToRedis(String menuCode) {
        //清空菜单按钮缓存
        if (null == menuCode) {
            redisOperator.mdel(redisOperator.keys(REDIS_MENU_BUTTON.value + ":*"));
        } else {
            redisOperator.mdel(redisOperator.keys(REDIS_MENU_BUTTON.value + ":" + menuCode));
        }
        List<SysMenuButtonVO> sysMenuButtonVOList = sysRoleMapperCustom.getMenuButtonByOn(menuCode);
        Iterator<SysMenuButtonVO> iterator = sysMenuButtonVOList.iterator();
        while (iterator.hasNext()) {
            SysMenuButtonVO sysMenuButtonVO = iterator.next();
            String key = REDIS_MENU_BUTTON.value + ":" + sysMenuButtonVO.getMenuCode();
            redisOperator.sadd(key, sysMenuButtonVO.getButtonCode());
        }
    }

    public void onLoadSysInfo() {
        sysInfoMapperCustom.truncateSysInfo();
        Map<String, Object> sysInfo = new HashMap<>();
        // ==== 服务器信息 ====
        SysInfoUtils.setServerInfo(sysInfo);
        //==== Java信息 ====
        SysInfoUtils.setJavaInfo(sysInfo);
        //==== CPU信息 ====
        SysInfoUtils.setCpuInfo(sysInfo);
        //==== Mem信息 ====
        SysInfoUtils.setMemInfo(sysInfo);
        //==== 硬盘信息 ====
        SysInfoUtils.setHardDiskInfo(sysInfo);

        sysInfoMapperCustom.insertSysInfoByBatch(sysInfo);
    }
}
